﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.IO;

public partial class Admin_AdminProductIphoneUpdate : System.Web.UI.Page
{
    string connectionString;
    private SqlConnection connect;
    private SqlDataAdapter da;
    private SqlCommand command;
    private DataSet ds = new DataSet();
    string id, name, type, price, img, imageDetails = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString = WebConfigurationManager.ConnectionStrings["AppleShopConnectionString"].ToString();
        connect = new SqlConnection(connectionString);
        command = new SqlCommand();
        if(!IsPostBack)
        {
            id = Request.QueryString["ID"];
            name = Request.QueryString["Name"];
            type = Request.QueryString["Type"];
            price = Request.QueryString["Price"];
            img = Request.QueryString["Image"];
            txtIphoneID.Text = id;
            txtIphoneName.Text = name;
            txtIphoneType.Text =type;
            txtIphonePrice.Text = price;
            Image1.ImageUrl = img;
            bindDataToDroDownIphoneDetails(id);
        }
    }
    protected void getUpdateIphone(string id2, string name2, float price2, string img2)
    {
        if (connect.State == ConnectionState.Open)
        {
            connect.Close();
        }
        connect.Open();
        command = new SqlCommand("spUpdateProductIphone", connect);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@ID", SqlDbType.VarChar).Value = id2;
        command.Parameters.Add("@Name", SqlDbType.VarChar).Value = name2;
        command.Parameters.Add("@Price", SqlDbType.Float).Value = price2;
        command.Parameters.Add("@Image", SqlDbType.VarChar).Value = img2;
        command.ExecuteNonQuery();
        connect.Close();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string filename = "";
        bool check = false;
       
        string id1, name1, img1;
        float price1;
        id1 = txtIphoneID.Text;
        name1 = txtIphoneName.Text;
        price1 = float.Parse(txtIphonePrice.Text);
        
        
        if (fileUploadIphone.HasFile)
        {
            string[] validate = { "bmp", "jpg", "jpeg", "png", "gif" };
            string ext = Path.GetExtension(fileUploadIphone.PostedFile.FileName);

            for (int i = 0; i < validate.Length; i++)
            {
                if (ext == "." + validate[i])
                {
                    if (fileUploadIphone.PostedFile.ContentLength <= 512000)
                    {
                        check = true;
                       
                        break;
                    }
                }
            }
            if (check)
            {
                filename = Path.GetFileName(fileUploadIphone.FileName);
                fileUploadIphone.SaveAs(Server.MapPath("~/IMG/iPhone/") + filename);
                lblStatus.Text = "Update Successfully";

                lblStatus.Visible = true;
                //.ImageUrl = "~/IMG/" + filename;
                //Image1.Visible = true;
            }
            else
            {
                lblStatus.Text = "File must be Images and less than 500kb";
                lblStatus.Visible = true;
            }
        }

        if (check == true)
        {


            img1 = "~/IMG/iPhone/" + filename;
            getUpdateIphone(id1, name1, price1, img1);
            Image1.ImageUrl = img1;
            lblStatus.Text = "Update Successfully";
            //lblStatus.Text = "" + id1 + " " + name1 + " " + price1 + " " + img1;
        }
        else if (check == false)
        {


            img1 = Request.QueryString["Image"];
            getUpdateIphone(id1, name1, price1, img1);
            Image1.ImageUrl = img1;
            lblStatus.Text = "Update Successfully";
            //lblStatus.Text = "" + id1 + " " + name1 + " " + price1 + " " + img1;
        }
    }
    protected void btnSelectIphoneDetails_Click(object sender, EventArgs e)
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
           // ds = new DataSet();
            command = new SqlCommand("spGetProductIphoneDetailsByNo", connect);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@No", SqlDbType.Int).Value = int.Parse(dropDownIphoneDetails.Text);
            da = new SqlDataAdapter(command);
            da.Fill(ds , "IphoneDetails"); 
            
            txtIphoneDetailsID.Text = ds.Tables["IphoneDetails"].Rows[0][1].ToString();
            txtIphoneDetailsText.Text = ds.Tables["IphoneDetails"].Rows[0][3].ToString();
            Image2.ImageUrl = ds.Tables["IphoneDetails"].Rows[0][2].ToString();
            txtIphoneDetailsNo.Text = ds.Tables["IphoneDetails"].Rows[0][0].ToString();
            imageDetails = ds.Tables["IphoneDetails"].Rows[0][2].ToString();
            btnUpdateIphoneDetails.Enabled = true;
            btnDeleteDetails.Enabled = true;
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);

        }
        finally
        {
            connect.Close();
        }
    }
    protected void getUpdateIphoneDetails(int no, string img, string text)
    {
        if (connect.State == ConnectionState.Open)
        {
            connect.Close();
        }
        connect.Open();
        command = new SqlCommand("spUpdateProductIphoneDetails", connect);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@No", SqlDbType.Int).Value = no;
        command.Parameters.Add("@Image", SqlDbType.VarChar).Value = img;
        command.Parameters.Add("@Text", SqlDbType.VarChar).Value = text;
        command.ExecuteNonQuery();
        connect.Close();
    }
    protected void btnUpdateIphoneDetails_Click(object sender, EventArgs e)
    {
        string filename1 = "";
        bool check = false;

        string textDetails, imgDetails;
        int NoDetails = 0;
        NoDetails = int.Parse(txtIphoneDetailsNo.Text);
        textDetails = txtIphoneDetailsText.Text;

     

        if (fileUploadIphoneDetails.HasFile)
        {
            string[] validate = { "bmp", "jpg", "jpeg", "png", "gif" };
            string ext = Path.GetExtension(fileUploadIphoneDetails.PostedFile.FileName);

            for (int i = 0; i < validate.Length; i++)
            {
                if (ext == "." + validate[i])
                {
                    if (fileUploadIphoneDetails.PostedFile.ContentLength <= 512000)
                    {
                        check = true;

                        break;
                    }
                }
            }
            if (check)
            {
                filename1 = Path.GetFileName(fileUploadIphoneDetails.FileName);
                fileUploadIphoneDetails.SaveAs(Server.MapPath("~/IMG/iPhone/") + filename1);
                //lblStatus.Text = "Update Successfully";

                lblStatus.Visible = true;
                //.ImageUrl = "~/IMG/" + filename;
                //Image1.Visible = true;
            }
            else
            {
                lblStatus.Text = "File must be Images and less than 500kb";
                lblStatus.Visible = true;
            }
        }

        if (check == true)
        {
            imgDetails = "~/IMG/iPhone/" + filename1;
            getUpdateIphoneDetails(NoDetails,imgDetails, textDetails);
            Image2.ImageUrl = imgDetails;
            lblStatus.Text = "Update Successfully";
            //lblStatus.Text = "" + id1 + " " + name1 + " " + price1 + " " + img1;
        }
        else if (check == false)
        {
            //imgDetails = imageDetails;
            getUpdateIphoneDetails(NoDetails, imageDetails, textDetails);
            Image2.ImageUrl = imageDetails;
            lblStatus.Text = "Update Successfully";
            //lblStatus.Text = "" + id1 + " " + name1 + " " + price1 + " " + img1;
        }
    }
    protected void bindDataToDroDownIphoneDetails(string ID)
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            //ds = new DataSet();
            command = new SqlCommand("spGetProductIphoneDetails", connect);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@ID", SqlDbType.VarChar).Value = ID;
            da = new SqlDataAdapter(command);
            da.Fill(ds, "details");
            int a = ds.Tables["details"].Rows.Count;
            dropDownIphoneDetails.Items.Clear();
            for (int i = 0; i < a; i++)
            {
                dropDownIphoneDetails.Items.Add(ds.Tables["details"].Rows[i][0].ToString());
            }

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);

        }
        finally
        {
            connect.Close();
        }
    }
    protected void dropDownIphoneDetails_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void getDeleteIphone(string ID)
    {
        if (connect.State == ConnectionState.Open)
        {
            connect.Close();
        }
        connect.Open();
        command = new SqlCommand("spDeleteIphone", connect);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@ID", SqlDbType.NVarChar).Value = ID;
        command.ExecuteNonQuery();
        connect.Close();
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        getDeleteIphone(Request.QueryString["ID"]);
        Response.Redirect("AdminProductIphone.aspx");
    }
    protected void getDeleteIphoneDetails(int No)
    {
        if (connect.State == ConnectionState.Open)
        {
            connect.Close();
        }
        connect.Open();
        command = new SqlCommand("spDeleteIphoneDetails", connect);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@No", SqlDbType.Int).Value = No;
        command.ExecuteNonQuery();
        lblStatus.Text = "Delete Product Details " + txtIphoneDetailsNo.Text + "successfull";
        connect.Close();
    }
    protected void btnDeleteDetails_Click(object sender, EventArgs e)
    {
        getDeleteIphoneDetails(int.Parse(txtIphoneDetailsNo.Text));

        
            bindDataToDroDownIphoneDetails(Request.QueryString["ID"].ToString());
        
    }
}